package com.kuang.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.kuang.pojo.User;
import com.kuang.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

//@Controller
//@ResponseBody
@RestController
public class UserController {
    @RequestMapping( "/json1")
//    @ResponseBody
    public String json1() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User("林东松我爱你", 99, "男");
        String str = mapper.writeValueAsString(user);
        return str;

    }
    @RequestMapping("/json2")
    public String json2 () throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        User user1 = new User("林东松1号", 1, "男");
        User user2 = new User("林东松2号", 1, "男");
        User user3 = new User("林东松3号", 1, "男");
        User user4 = new User("林东松4号", 1, "男");
        ArrayList<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        String s = mapper.writeValueAsString(users);
        return s;
    }
    @RequestMapping("/json3")
    public String json3() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Date date = new Date();
        String s = mapper.writeValueAsString(date);
        return s;
    }
    @RequestMapping("/json4")
    public String json4() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
          mapper.setDateFormat(sdf);
        Date date = new Date();
        String s = mapper.writeValueAsString(date);
        return s;
    }
    @RequestMapping("/json5")
    public String json5(){
        Date date = new Date();
        String json = JsonUtils.getJson(date);
        return json;
    }
}
